<script setup>
import { getHotRecommendAPI } from '@/services/home'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import SokachHotSkeleton from '@/components/sokach-hot-skeleton.vue'

//接收数据
const props = defineProps({
  type: String,
})

// 热门推荐页 标题和url
const hotMap = [
  { type: '1', title: '特惠推荐', url: '/hot/preference' },
  { type: '2', title: '爆款推荐', url: '/hot/inVogue' },
  { type: '3', title: '一站买全', url: '/hot/oneStop' },
  { type: '4', title: '新鲜好物', url: '/hot/new' },
]

//找到对应参数的数据
const currHot = hotMap.find((item) => item.type === props.type)

//动态设置标题
uni.setNavigationBarTitle({ title: currHot.title })

//获取推荐的热门数据
const bannerPicture = ref('') // 推荐封面图
const subTypes = ref([]) // 推荐选项
const getHotRecommendData = async () => {
  const res = await getHotRecommendAPI(currHot.url, {
    page: 1,
    pageSize: 10,
  })

  // 保存封面
  bannerPicture.value = res.result.bannerPicture

  // 保存列表
  subTypes.value = res.result.subTypes
}

//默认tabs
const tabActive = ref(0)

//点击切换tabs
const onChangeTabActive = (index) => {
  tabActive.value = index
}

//加载状态
const isloading = ref(false)

//页面加载
onLoad(async () => {
  isloading.value = true
  await getHotRecommendData()
  isloading.value = false
})

// 滚动触底
const onScrolltolower = async () => {
  //拿到当前活跃的数据
  const activeSubTypes = subTypes.value[tabActive.value]

  console.log(activeSubTypes)

  //进行分页条件判断
  if (activeSubTypes.goodsItems.page < activeSubTypes.goodsItems.pages) {
    // 当前页码累加
    activeSubTypes.goodsItems.page++
  } else {
    // 标记已结束
    activeSubTypes.finish = true
    // 退出并轻提示
    return uni.showToast({ icon: 'none', title: '没有更多数据了~' })
  }

  //调用接口
  const res = await getHotRecommendAPI(currHot.url, {
    subType: activeSubTypes.id,
    page: activeSubTypes.goodsItems.page,
    pageSize: activeSubTypes.goodsItems.pageSize,
  })

  //新的列表
  const newList = res.result.subTypes[tabActive.value]

  //列表追加
  activeSubTypes.goodsItems.items = [
    ...activeSubTypes.goodsItems.items,
    ...newList.goodsItems.items,
  ]
}
</script>

<template>
  <view class="hot">
    <SokachHotSkeleton v-if="isloading"></SokachHotSkeleton>
    <template v-else>
      <view class="top">
        <!-- 推荐封面 -->
        <view class="cover"> <image class="image" :src="bannerPicture" /></view>
        <!-- 推荐选项 -->
        <view class="tabs">
          <text
            class="text"
            v-for="(item, index) in subTypes"
            :class="tabActive === index ? 'active' : ''"
            @click="onChangeTabActive(index)"
            >{{ item.title }}</text
          >
        </view>
      </view>
      <!-- 推荐列表 -->
      <scroll-view
        scroll-y
        v-for="(item, index) in subTypes"
        :key="item.id"
        v-show="tabActive === index"
        @scrolltolower="onScrolltolower"
      >
        <view class="goods">
          <navigator
            class="item"
            v-for="goods in item.goodsItems.items"
            :key="goods.id"
            :url="`/pages/goods/goods?id=${goods.id}`"
          >
            <image class="thumb" :src="goods.picture" />
            <view class="name ellipsis"> {{ goods.name }}</view>
            <view class="price">
              <text class="symbol">¥</text>
              <text class="number">{{ goods.price }}</text>
            </view>
          </navigator>
        </view>
        <text class="loading-text"> {{ item.finish ? '没有更多数据了~' : '正在加载...' }}</text>
      </scroll-view>
    </template>
  </view>
</template>

<style lang="scss">
page {
  height: 100%;
  background-color: #f4f4f4;
}
.hot {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 180rpx 0 0;
  position: relative;
}
.cover {
  width: 750rpx;
  height: 225rpx;
  border-radius: 0 0 40rpx 40rpx;
  overflow: hidden;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 9;
}
.image {
  width: 100%;
  height: 100%;
}

.tabs {
  display: flex;
  justify-content: space-evenly;
  height: 100rpx;
  line-height: 90rpx;
  background-color: #c44242;
  font-size: 28rpx;
  border-radius: 10rpx;
  box-shadow: 0 4rpx 5rpx rgba(200, 200, 200, 0.3);
  margin: 0 20rpx;
  color: #333;
  background-color: #fff;
  position: relative;
  z-index: 9;
  .text {
    margin: 0 20rpx;
    position: relative;
  }
  .active {
    &::after {
      content: '';
      width: 40rpx;
      height: 4rpx;
      background-color: #27ba9b;
      transform: translate(-50%);
      background-color: #27ba9b;
      position: absolute;
      left: 50%;
      bottom: 24rpx;
    }
  }
}
.scroll-view {
  flex: 1;
  padding-top: 8000px;
}
.goods {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 20rpx 20rpx;
  .item {
    width: 345rpx;
    padding: 20rpx;
    margin-top: 20rpx;
    border-radius: 10rpx;
    background-color: #fff;
  }
  .thumb {
    width: 305rpx;
    height: 305rpx;
  }

  .name {
    height: 88rpx;
    font-size: 26rpx;
  }
  .price {
    line-height: 1;
    color: #cf4444;
    font-size: 30rpx;
  }
}
.loading-text {
  display: flex;
  justify-content: center;
  font-size: 28rpx;
  color: #666;
  padding: 20rpx 0 50rpx;
}
</style>
